import {createRouter,createWebHashHistory,RouteRecordRaw} from 'vue-router'
import pinia from '../store/pinia'
import { userStore } from '../store/user'
const store = userStore(pinia)
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'


NProgress.configure({ 
    easing: 'ease', // 动画方式
    speed: 500, // 递增进度条的速度
    showSpinner: false, // 是否显示加载ico
    trickleSpeed: 200, // 自动递增间隔
    minimum: 0.3,// 初始化时的最小百分比,
})
const routes:Array<RouteRecordRaw> = [
   {
        path: "/",
        redirect: "/home"
    },
    {
        path:"/home",
        component: () => import("../views/Home.vue"),
        name: "Home",
        meta: {
            isShow: true,
            keepAlive: true
        }
    },{
        path: "/admin",
        component: () => import("../views/Admin/Admin.vue"),
        children: [
            {
                path:"",
                component: () => import("../views/Admin/AdminIndex.vue"),
                meta: {
                    isShow: true
                }
            },

            {
                path: "data",
                component: () => import("../views/Admin/ManageData.vue"),
                meta: {
                    isShow: true
                }
            },
            {
                path: "state",
                component: () => import("../views/Admin/State.vue"),
                meta: {
                    isShow: true
                }
            },
            // {
            //     path: "watch",
            //     component: () => import("../views/Admin/Watch.vue")
            // }
            {
                path: "user",
                component: () => import("../views/Admin/User.vue"),
                meta: {
                    isShow: true
                }
            },{
                path: "analysis",
                component: () => import("../views/Admin/analysis.vue"),
                meta: {
                    isShow: true
                }
            },
            {
                path: "setting",
                component: () => import("../views/Admin/Setting.vue")
            },
            {
                path: "message",
                component: () => import("../views/Admin/Message.vue"),
                meta: {
                    isShow: true
                }
            },
            // {
            //     path: "gateway",
            //     component: () => import("../views/Admin/Gateway.vue"),
            // },
            // {
            //     path: "debug",
            //     component: () => import("../views/Admin/Debug.vue"),
            // }
        ]
    },
    {
        path: "/login",
        component: () => import("../views/Login.vue"),
        name: "Login"
    }
]

const router = createRouter({
    routes,
    history: createWebHashHistory()
})

// 路由守卫
// router.beforeEach(async (to, from, next) => {
//     NProgress.start()
//     // 新增：已登录访问 /login 自动跳转到首页
//     if (to.fullPath === '/login') {
//         if (store.isLogin || localStorage.getItem('isLogin') === 'true') {
//             return await next('/home')
//         }
//         return await next()
//     }
//     // 原有逻辑
//     if (to.fullPath !== '/login') {
//         // 检查 Pinia store 中的登录状态
//         if (store.isLogin) {
//             return await next()
//         } 
//         // 如果 Pinia store 中没有登录状态，检查 localStorage
//         const isLoginFromLocalStorage = localStorage.getItem("isLogin") === 'true';
//         const userInfoFromLocalStorage = localStorage.getItem("userInfo");

//         if (isLoginFromLocalStorage && userInfoFromLocalStorage) {
//             // 如果 localStorage 中有登录信息，恢复 store 状态
//             try {
//                 store.initUser(JSON.parse(userInfoFromLocalStorage));
//                 return await next();
//             } catch (e) {
//                 console.error("Failed to parse user info from localStorage:", e);
//                 // 解析失败，清除 localStorage 并重定向到登录页
//                 localStorage.removeItem("isLogin");
//                 localStorage.removeItem("userInfo");
//                 return await next("/login");
//             }
//         } else {
//             // localStorage 中也没有登录信息，重定向到登录页
//             return await next("/login");
//         }
//     } else {
//         // 目标路径是登录页，直接允许跳转
//         return await next();
//     }  
// })

// router.afterEach(() => {
//     NProgress.done()
// })
export default router